/**
 * Created by lt on 2017/5/12.
 */
/*查询所有菜单*/
var code;
var zNodes = [];
var setting = {
    check: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    }
    , callback: {
        onClick: zTreeOnClick
    }
};
$(function () {
    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    setCheck();
    $("#py").bind("change", setCheck);
    $("#sy").bind("change", setCheck);
    $("#pn").bind("change", setCheck);
    $("#sn").bind("change", setCheck);
    getMenuList();
});

function setCheck() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
        py = $("#py").attr("checked") ? "p" : "",
        sy = $("#sy").attr("checked") ? "s" : "",
        pn = $("#pn").attr("checked") ? "p" : "",
        sn = $("#sn").attr("checked") ? "s" : "",
        type = {"Y": py + sy, "N": pn + sn};
    zTree.setting.check.chkboxType = type;
    showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
}

function showCode(str) {
    if (!code) code = $("#code");
    code.empty();
    code.append("<li>" + str + "</li>");
}

function zTreeOnClick(event, treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("#treeDemo");
    var id = treeNode.id;
    var menuParentId = treeNode.pId;
    if (menuParentId == null) {
        menuParentId = id;
    }
    $("#menuList").val(menuParentId);
}

function getIds() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    var nodes = zTree.getCheckedNodes(true);
    var ids = [];
    if (nodes) {
        var length = nodes.length;
        for (var i = 0; i < length; ++i) {
            var node = nodes[i];
            ids.push(node.id);
        }
    }
    return ids.join(",");
}

function getMenuList() {
    $.ajax({
        type: 'post',
        url: '/admin/menu/getMenuList',
        data: {},
        success: function (data) {
            var data = $.parseJSON(data);
            console.log(data);
            if (data.code != 1) {
                alert(data.msg);
                return;
            }
            var ddd = [];
            for (i = 0; i < data.data.length; i++) {
                var menuParentId = data.data[i].menuParentId;
                var d = {id: data.data[i].tid, pId: menuParentId, name: data.data[i].menuName, open: open};
                ddd.push(d);
            }
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getNodesByParam("id", "ssss", null);
            treeObj.addNodes(nodes[0], ddd);
        }
    });
}

/*菜单-添加*/
$(function () {
    $("#form-user-add").validate({
        rules: {
            menuName: {
                required: true
            },
        },
        onkeyup: false,
        focusCleanup: true,
        success: "valid",
        submitHandler: function (form) {
            $("#form-user-add").ajaxSubmit({
                type: 'post',
                url: '/admin/menu/addMenu.html',
                data: $("#form-user-add").serialize(),
                dataType: 'json',
                success: function (data) {

                    if (data.code != 1) {
                        layer.msg('操作失败!', {icon: 5, time: 1000});
                        console.log(data.msg);
                        return;
                    }
                    layer.msg('操作成功!', {icon: 6, time: 1000}, function () {
                        window.location.reload();
                    })
                }
            })
        }
    });
});

/*菜单-删除*/
function member_del() {
    layer.confirm('确认要删除吗？', function (index) {
        var menuIds = getIds();
        var data = {menuIds: menuIds}
        $.ajax({//获取用户id
            type: 'post',
            url: '/role/deleteMenu.html',
            data: data,
            success: function (data) {
                var data = $.parseJSON(data);
                if (data.code != 1) {
                    layer.msg('操作失败!', {icon: 5, time: 1000});
                    console.log(data.msg);
                    return;
                }
                layer.msg('操作成功!', {icon: 6, time: 1000}, function () {
                    window.location.reload();
                })
            }
        });
    });
}
